package com.wsjc.app.data.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wsjc.app.data.App;
import com.wsjc.common.core.constants.CommonConst;
import com.wsjc.manager.entity.WjDataLog;

/**
 * <b>功能说明:数据处理任务 TODO... 无注释
 */
public class DataTask {

	private static Logger logger = LoggerFactory.getLogger(DataTask.class);

	private WjDataLog wjDataLog;

	private DataPersist dataPersist = App.dataPersist;

	public DataTask() {}

	public DataTask(WjDataLog wjDataLog) {
		super();
		this.wjDataLog = wjDataLog;
	}

	/**
	 * 执行更新map.
	 */
	public void run(int executeCount, int executeMaxCount) {

		// 去跟新
		try {
			/** 获取灭菌效果实体，需要动态识别表明 **/
			dataPersist.flushData(wjDataLog.getId(), wjDataLog.getDataId(), wjDataLog.getDataType(), wjDataLog.getHospitalId());
			
			// 重置为0，开始下一个task的处理
			dataPersist.updateDataLog(wjDataLog, CommonConst.DATA_STATUS_SUCCESS);
			executeCount = 0;

		} catch (Exception e) {

			if (executeCount > executeMaxCount) {
				// 失败3次后，db mapStatus 为 fail , executeCount 重置为0，处理下一条task.
				executeCount = 0;
				dataPersist.updateDataLog(wjDataLog, CommonConst.DATA_STATUS_FAIL);
				logger.warn("app-map|flush map error for executeCount is max count, so we need updata map_status to faild at wj_data_log table.");
				return;
			}
			logger.error("app-map|flush map error, executeCount={}, error stack info: {}", executeCount, e); 
			executeCount++;
			return; 

		}

	}

}
